﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CalculationInterval
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //全局变量
        int rowIndex;
        //增加行
        private void dataGridView2_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.dataGridView2.Rows.Add();
            }
        }
        //删除行
        private void dataGridView2_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (!this.dataGridView2.Rows[this.rowIndex].IsNewRow)
                {
                    this.dataGridView2.Rows.RemoveAt(dataGridView2.CurrentRow.Index);
                }
            }
        }
        //获取单元格信息
        private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //获取单元格坐标
            int col = dataGridView2.CurrentCellAddress.X;
            int row = dataGridView2.CurrentCellAddress.Y;

            //获取单元格内容
            //string content = dataGridView2.CurrentCell.Value.ToString();

            //MessageBox.Show("行:" + row.ToString() + " 列:" + col.ToString() + " 内容:" + content);
        }
        //托管控件
        public DataGridViewTextBoxEditingControl CellEdit = null;
        private void dataGridView2_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            //获取当前处于活动状态的单元格索引
            if ((this.dataGridView2.CurrentCellAddress.X == 0) || (this.dataGridView2.CurrentCellAddress.X == 1) || (this.dataGridView2.CurrentCellAddress.X == 2))
            {
                CellEdit = (DataGridViewTextBoxEditingControl)e.Control;
                CellEdit.SelectAll();
                //绑定事件
                CellEdit.KeyPress += Cells_KeyPress;
            }
        }
        //自定义事件，判断格式是否正确
        private void Cells_KeyPress(object sender, KeyPressEventArgs e)
        {
            //获取当前处于活动状态的单元格索引
            if ((this.dataGridView2.CurrentCellAddress.X == 1) || (this.dataGridView2.CurrentCellAddress.X == 2))
            {
                //判断输入区间格式是否正确
                if (!(e.KeyChar >= '0' && e.KeyChar <= '9'))
                {
                    e.Handled = true;
                }
                if (e.KeyChar == '\b')
                {
                    e.Handled = false;
                }
            }
            //获取前缀单元格索引
            if ((this.dataGridView2.CurrentCellAddress.X == 0))
            {
                //判断前缀格式是否正确
                if (!(e.KeyChar >= 'A' && e.KeyChar <= 'z'))
                {
                    e.Handled = true;
                }
                if (e.KeyChar == '\b')
                {
                    e.Handled = false;
                }
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //前缀名称不能为空
            for (int i = 0; i < dataGridView2.Rows.Count; i++)
            {
                string value = (string)dataGridView2.Rows[i].Cells[0].Value;
                //判断前缀是否为空或空字符串
                if (value == "" || value == null)
                {
                    MessageBox.Show("前缀名称不能为空!", "错误");
                    return;
                }
            }
            string AreaLength = textBox1.Text.Trim();
            double Length;

            //判断用户输入区间长度格式是否正确
            if (double.TryParse(AreaLength, out Length))
            {
                Console.WriteLine("用户输入的区间长度为:{0}", Length);
            }
            else
            {
                MessageBox.Show("输入区间长度错误，请重新输入");
                textBox1.Text = "";
                return;
            }
            //文件位置
            string FullFileName = @"D:\区间数据.txt";
            //获取跨度区间
            double Interval = double.Parse(textBox1.Text.Trim());
            //声明初始区间为0
            double Num1 = 0;
            //声明全局变量次数
            int NumberOfTimes = -1;
            //创建文件流
            StreamWriter sw = new StreamWriter(FullFileName, true, Encoding.Default);
            //接收需要输出的文本保存
            string str = "";
            //接收需要输出的备注文本保存
            string Remarks = "";
            //接收文本框变量
            for (int i = 0; i < dataGridView2.Rows.Count; i++)
            {
                //获取前缀，起点，终点三个值
                string Prefix = dataGridView2.Rows[i].Cells[0].Value.ToString().Trim();
                double StartingPoint = double.Parse(dataGridView2.Rows[i].Cells[1].Value.ToString());
                double EndPoint = double.Parse(dataGridView2.Rows[i].Cells[2].Value.ToString());
                while (true)
                {
                    //判断是否在区间
                    if (Interval >= StartingPoint && Interval <= EndPoint)
                    {
                        //判断当前区间是不是第一次循环
                        if (NumberOfTimes != -1)
                        {
                            //给备注赋初始值
                            Remarks = "-无";
                            str = Prefix + "," + Num1 + "," + Prefix + "," + Interval + Remarks + "\r\n";
                            Remarks = "";
                        }
                        else
                        {
                            //判断备注是否为空，为空说明没有跨越区间正常进入下一个区间
                            if (Remarks == "")
                            {
                                Remarks = "-无";
                                str = str + "," + Prefix + "," + Interval + Remarks + "\r\n";
                            }
                            //不为空证明跨越过区间，备注应该是有值的
                            else
                            {
                                str = str + "," + Prefix + "," + Interval + Remarks + "\r\n";
                            }
                        }
                        //区间值增加
                        Num1 = Num1 + double.Parse(textBox1.Text.Trim());
                        Interval = Interval + double.Parse(textBox1.Text.Trim());
                        sw.Write(str);
                        //备注文本改为空
                        str = "";
                        //次数增加
                        ++NumberOfTimes;
                    }
                    else
                    {
                        if (NumberOfTimes == -1)
                        {
                            //记录备注区间值
                            str = dataGridView2.Rows[i].Cells[0].Value.ToString().Trim() + "," + 0;
                            Num1 = Num1 + double.Parse(textBox1.Text.Trim());
                            Interval = Interval + double.Parse(textBox1.Text.Trim());
                            continue;
                        }
                        //判断是否第一次跨越区间
                        if (NumberOfTimes != -1)
                        {
                            //记录备注区间值
                            str = str + dataGridView2.Rows[i].Cells[0].Value.ToString().Trim() + "," + Num1;
                            NumberOfTimes = 0;
                        }
                        else
                        {
                            //记录备注区间值
                            Remarks = Remarks + Prefix + "," + StartingPoint + "," + EndPoint + ",";
                            if (Remarks == null || Remarks == "")
                            {
                                Remarks = "-无";
                            }
                        }
                        break;
                    }
                }
            }
            sw.Close();
        }
    }
}
